package com.sunday.ble.orlant.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.sunday.ble.orlant.R;
import com.sunday.ble.orlant.greendao.Device;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by littlexs on 2017/2/23.
 */

public class HistoryAdapter extends RecyclerView.Adapter {

    private Context mContext;
    private List<Device> deviceList;
    private int count;

    public HistoryAdapter(Context mContext, List<Device> deviceList) {
        this.mContext = mContext;
        this.deviceList = deviceList;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.history_blue_item, parent, false);
        return new ViewHolder1(view);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        ViewHolder1 holder1 = (ViewHolder1) holder;
        final Device device = deviceList.get(position);
        if (getItemCount()>1){
            if (position==0){
                holder1.lMain.setBackgroundResource(R.drawable.history_item02);
            }else if (position==getItemCount()-1){
                holder1.lMain.setBackgroundResource(R.drawable.history_item01);
            }else {
                holder1.lMain.setBackgroundResource(R.drawable.history_item03);
            }
        }else {
            holder1.lMain.setBackgroundResource(R.drawable.history_item04);
        }
        holder1.name.setText(device.getName());
        holder1.updateName.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener!=null){
                    listener.itemClick(position,device);
                }
            }
        });
    }
    private OnItemListener listener;

    public interface OnItemListener{
        void itemClick(int position,Device device);
    }

    public void refresh(List<Device> list){
        this.deviceList = list;
        this.notifyDataSetChanged();
    }

    public void setOnItemListener(OnItemListener listener){
        this.listener = listener;
    }

    @Override
    public int getItemCount() {
        return deviceList.size();
    }

    public class ViewHolder1 extends RecyclerView.ViewHolder {
        @Bind(R.id.name)
        TextView name;
        @Bind(R.id.updateName)
        ImageView updateName;
        @Bind(R.id.l_main)
        LinearLayout lMain;
        public ViewHolder1(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
